<?php
namespace Dev\Controller;
use Common\Model\GameModel;
use Think\Controller;
use Think\Page;
use Dev\BaseController;
class SysUserController extends BaseController{

	/*
	 * function index 管理员列表
	 */
	public function index(){
		$sysUser = M("sys_user",'',DB_GM_CFG);
		$sysRole = M("sys_role",'',DB_GM_CFG);

		import("ORG.Util.Page");
		$count = $sysUser->count();
		$Page = new Page($count,100);
		fetchPage($this->smarty,$Page);

		$list = $sysUser->limit($Page->firstRow,$Page->listRows)
			              ->field('role_id,username,gm_sys_user.id,real_name,status,uid,nickname')
						  ->select();
		$this->smarty->assign('list',$list);

		$roleList=$sysRole->select();
		$roleKV=array();
		foreach($roleList as $role){
			$roleKV[$role['id']]=$role['remark'];
		}

		$this->smarty->assign('rolelist',$roleKV);
		$this->smarty->display('SysUser/index.html');

	}


	/*
	 * 添加页面
	 */
	public function add(){
		import("@.Action.Home.SysRoleAction");
		$sysRoleAction = A("SysRole");
		$roleList = $sysRoleAction->getRoleList();
		$this->smarty->assign('role_list',$roleList);
		$this->smarty->assign('opt',1);

		$gameKV=GameModel::getGameKv();
		$this->smarty->assign('games',$gameKV);

		$this->smarty->display('SysUser/edit.html');
	}

	/**
	 * 添加
	 */
	public function insert(){
		$sysUserM = M("sys_user",'',DB_GM_CFG);


		$real_name = I('post.real_name', null ,'htmlspecialchars');
		$username = I('post.username', null ,'htmlspecialchars');
		$password = I('post.password', null ,'md5');
		$role_id = I('post.role_id', 0, 'intval');
		$status = I('post.status', 0, 'int');
		$selectedGames = I('post.games');

		/*在创建管理员前对表单字段进行判断*/

		//判断登陆账号是否存在
		if($sysUserM->where("username='{$username}'")->find()){
			$this->error('账号已存在');
		}
		

		if($vo = $sysUserM->create()) {
			$vo['real_name']=$real_name;
			$vo['username']	=$username;
			$vo['password']	=$password;

			$vo['role_id']	=$role_id;
			$vo['status']	=$status;
			$vo['games']	=implode(',',$selectedGames);

			$b=$sysUserM->add($vo);
			if($b){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->error($sysUserM->getError());
		}
	}

	/**
	 * 修改页面显示
	 */
	public function edit(){
		$id = I('get.id',0,'int');
		if($id ){
			//根据用户ID获得角色，供前台显示
			$sysUser = M("sys_user",'',DB_GM_CFG);

			$result	= $sysUser->find($id);
			$this->smarty->assign('obj',$result);

			$gameList=explode(',',$result['games']);
			$this->smarty->assign('selectedGame',$gameList);

			$sysRoleM = M("sys_role",'',DB_GM_CFG);
			$this->smarty->assign('role_list',$sysRoleM->select());
		}

		$this->smarty->assign('opt',0);

		$gameKV=GameModel::getGameKv();
		$this->smarty->assign('games',$gameKV);


		$this->smarty->display('SysUser/edit.html');

	}

	/**
	 * 修改
	 */
	public function update(){
		$sysUserM = M("sys_user",'',DB_GM_CFG);

		$id = I('post.id', null, 'intval');
		$real_name = I('post.real_name', null ,'htmlspecialchars');
		$username = I('post.username', null ,'htmlspecialchars');
		$password = I('post.password',null);
		$role_id = I('post.role_id', 0, 'intval');
		$status = I('post.status', 0, 'int');
		$selectedGames = I('post.games');


		//获取更新前的用户的信息
		$prev_sysUser = $sysUserM->find($id);

		//判断修改后的登陆账号是否存在
		if($username != $prev_sysUser['username']){
			//判断登陆账号是否存在
			if($sysUserM->where("username='{$username}'")->find()){
				$this->error('账号已存在');
			}
		}


		if($data=$sysUserM->create()) {
			if($password){
				$data["password"] = md5($password);
			}else{
				unset($data["password"]);
			}
			$data['id']			= $id;
			$data['real_name']	= $real_name;
			$data['username']	= $username;
			$data['role_id']	= $role_id;
			$data['status']		= $status;
			$data['games']		= implode(',',$selectedGames);

			$b=$sysUserM->save($data);
			if($b!==false){
				$this->success('数据更新成功');
			}else{
				$this->error('数据更新失败');
			}
		}else{
			$this->error($sysUserM->getError());
		}

	}

	/**
	 * 删除
	 */
	public function delete(){
		$id = I('post.id', null, 'intval');
		//获取id 
		if($id) {
			$sysUser = M("sys_user",'',DB_GM_CFG);
			$result	= $sysUser->delete($id);
			if(false !== $result) {
				$this->success('删除管理员用户成功');
			}else{
				$this->error('删除管理员用户失败');
			}
		}else{
			$this->error('数据不存在');
		}

	}



	/*
	* 搜索
	*/
	public function search(){
		$sysUser = M("SysUser");
		$id = I('get.id',0,'int');
		$status = I('get.status',null,'int');
		$keywords = I('get.keywords',null);
		$where = " 1 = 1 ";
		if(!empty($id)){
			$where .= " and id = $id ";
		}

		if(!is_null($status)){
			$where .= " and status = '$status' ";
		}

		if(!empty($keywords)){
			$where .= " and real_name like '%$keywords%' ";
		}
		import("ORG.Util.Page");
		$count = $sysUser->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);
		$limit = I('get.limit',10,'int');
		$Page = new Page($count,$limit);
		fetchPage($this->smarty,$Page);
		$Page->parameter = "id=$id&status=$status&keywords=$keywords";
		$show = $Page->show(); // 分页显示输出
		$list = $sysUser->table("gm_sys_user")
						  ->where($where)
						  ->limit($Page->firstRow.','.$Page->listRows)
						  ->select();
		$this->smarty->assign('list',$list);

		$roleList=M("gm_sys_role",null,DB_GM_CFG)->select();
		$roleKV=array();
		foreach($roleList as $role){
			$roleKV[$role['id']]=$role['remark'];
		}

		$this->smarty->assign('rolelist',$roleKV);

		$this->smarty->assign('limit',$limit);
		$this->smarty->assign('keywords',$keywords);
		$this->smarty->assign('status',$status);
		$this->smarty->display("SysUser/index.html");
	}

	/*
	*	替换显示函数
	*	为了更友好的用户体验，替换一些数据为中文表示方法,比如status=1的数据，页面显示"启用"
	*/
	function replaceShowText($list){

		$i = 0;
		foreach($list as $l){
			if($l["status"]==1){
				$list[$i]["status"] = L("Open");
			}else{
				$list[$i]["status"] = "<font color='#FF0000'>".L("Close")."</font>";
			}
			$i++;
		}
		return $list;
	}

}
?>